<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Moto_marca_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->config('parametros');
    }

    /**
     * Get user record by Id
     *
     * @param	int
     * @param	bool
     * @return	object
     */
    function getAllMarcasOrderByNome() {
        $this->db->order_by("nome ASC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblMarca'));

        
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function getMarca($_id) {
        $this->db->where('id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblMarca'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function getMarcaByNome($_nome) {
        $this->db->where('nome', $_nome);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblMarca'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function inserir($_dados) {
        $this->db->set($_dados);
        $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblMarca'));
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return NULL;
        }
    }

    function atualizar($_id, $_dados) {
        $this->db->where('id', $_id);
        $this->db->set($_dados);
        $this->db->update($this->config->item('dbCliente') . $this->config->item('tblMarca'));
        if ($this->db->affected_rows() > 0) {
            return $_id;
        } else {
            return NULL;
        }
    }

    function getAllMarcasOrderById() {
        $this->db->order_by("id DESC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblMarca'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function combo() {
        $result = $this->getAllMarcasOrderByNome();
        
        if ($result != null){
            $option = array();
            $option[''] = 'Selecionar';
            foreach ($result as $value) {
                $option[$value->id] = $value->nome;
            }
            return $option;
        }
        return null;
    }
}